package com.chat.servlet;


import com.chat.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author: WFP
 * @Time: 2017/12/30
 * @decription:用于用户登录的servlet
 */
public class LoginSevlet extends HttpServlet {


    @Override
    public void init() throws ServletException {
        System.out.println("***Servlet初始化***");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //取得表单参数
        String userName = req.getParameter("username");
        String password = req.getParameter("password");

        //验证用户
        if(userName!=null&&password!=null) {
            if(("zs".equals(userName)&&"123".equals(password))
                    || ("ls".equals(userName)&&"123".equals(password))
                    ||("ww".equals(userName)&&"123".equals(password))){
                HttpSession session = req.getSession();
                session.setAttribute("uname",userName);
                resp.sendRedirect("chat.jsp");
            }else{
                req.getRequestDispatcher("fail.jsp").forward(req,resp);
            }
        }
    }

    @Override
    public void destroy() {
        System.out.println("***Servlet销毁***");
    }
}
